package com.wantong.jianpu.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.androidkun.xtablayout.XTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.wantong.jianpu.BR;
import com.wantong.jianpu.R;
import com.wantong.jianpu.adapters.HomePagerAdapter;
import com.wantong.jianpu.base.BaseXActivity;
import com.wantong.jianpu.beans.TabBean;
import com.wantong.jianpu.databinding.ActivityTourDetailBinding;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.event.TourDetailEvent;
import com.wantong.jianpu.fragments.TourDetailFragment;
import com.wantong.jianpu.viewmodels.TourDetailViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.views.MenuClick;

/**
 * 巡检详情
 */
public class TourDetailActivity extends BaseXActivity<ActivityTourDetailBinding, TourDetailViewModel> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_tour_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        setToolbar(binding.baseToolBar);
        EventBus.getDefault().register(this);
        viewModel.id=getIntent().getExtras().getString("id");

        viewModel.currentNum.observe(this, integer -> {
            initTab(integer);
        });

        viewModel.completeEvent.observe(this, aBoolean -> {
            new XPopup.Builder(TourDetailActivity.this).asConfirm("提示", "是否完成本次巡检", () -> {
                viewModel.completeInspection();
            }).show();
        });

    }

    private void initTab(int currentNum) {
        binding.tabLayout.removeAllTabs();
        for(int i=0;i<currentNum;i++){
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText("第"+(i+1)+"次"));
        }

            binding.tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(XTabLayout.Tab tab) {
                    viewModel.nowPosition.setValue(tab.getPosition()+1);
                    viewModel.setAdapterData();
                }

                @Override
                public void onTabUnselected(XTabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(XTabLayout.Tab tab) {

                }
            });

        runOnUiThread(() -> {
            binding.tabLayout.getTabAt(currentNum-1).select();
        });
        viewModel.nowPosition.setValue(currentNum-1);
        viewModel.setAdapterData();
    }

    /**
     * 刷新
     * @param event
     */
    @Subscribe
    public void onEvent(TourDetailEvent event){
        viewModel.getDeviceDetail();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}